<?php

namespace App\Console\Commands;

use App\Models\Email\EmailHistroy;
use Doctrine\DBAL\Driver\PDOException;
use Illuminate\Console\Command;
use Mockery\Exception;

class SqlMigration extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sql:checkMigration {--TableName=-1} {--number=-1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $TableName = $this->option('TableName');
       if('dichotomy' == $TableName){
            $number = $this->option('number');
            $this->dichotomy($number);
        }elseif('shard' == $TableName){
            $this->shard("mysql","MWS_List_Orders_latest");
           $this->shard("mysql","MWS_List_Orders_latest");
           $this->shard("mysql","MWS_List_Orders_latest");
           $this->shard("mysql","MWS_List_Orders_latest");
           $this->revises();
//           $this->shard("mysql","MWS_List_Orders");
//           $this->shard("mysql","mws_list_order_latest");
//           $this->shard("mysql","MWS_List_Items");
        }elseif('revises' == $TableName) {
            $this->revises();
        }elseif('redichotomy' == $TableName) {
           $this->redichotomy();
       }else {
            echo "start else".PHP_EOL;
        }

    }


    public function redichotomy(){
        $result = \DB::connection("mysql3310")->table("checktable")->where("poor","!=",0)
            ->get();
        foreach ($result as $item){
            $mysqlmiged = $item->newsql;
            $newtable = $item->newtable;
            $column = $item->column;
            $mysqlold = $item->oldsql;
            $oldtable = $item->oldtable;
            $uplingshi = $item->end;
            $downlingshi = $item->start;
            echo "uplishi and down lishi is :".$uplingshi." and ".$downlingshi.PHP_EOL;
            $numbermy = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            echo "my over".PHP_EOL;
            $number248 = \DB::connection($mysqlold)->table($oldtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            $cha = $number248-$numbermy;
            $item->poor = $cha;
            $item->oldnumber = $number248;
            $item->newnumber = $numbermy;
            if($cha == 0){
                $item->status = 1;
                $item->status2 = 1;
            }else{
                if($item->magnitude <= 10){
                    $item->status = 0;
                    $item->status2 = 1;
                }else{
                    $item->status = 0;
                    $item->status2 = 0;
                }
            }
            $item->FetchTime = time();
            $arr = get_object_vars($item);
            print_r($arr);
            unset($arr["id"]);
            \DB::connection("mysql3310")->table("checktable")->where("id",$item->id)->update($arr);
        }

    }


    public function  dichotomy($number){
        echo $number.PHP_EOL;
        if($number == 1){
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold  = 'mycat41'; //这是老的数据库
            $oldtable = 'MWS_List_Items';
            $newtable = 'MWS_List_Items';
            $column = 'FetchTime';
        }elseif ($number == 2) {
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold = 'mycat41'; //这是老的数据库
            $oldtable = 'MWS_List_Orders';
            $newtable = 'MWS_List_Orders';
            $column = 'Fetchtime';
        }elseif ($number == 3){
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold  = 'mycat41'; //这是老的数据库
            $oldtable = 'MWS_List_Orders_latest';
            $newtable = 'MWS_List_Orders_latest';
            $column = 'Fetchtime';
        }elseif ($number == 4){
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold  = 'mycat248'; //这是老的数据库;
            $oldtable = 'mws_list_item';
            $newtable = 'mws_list_item';
            $column = 'FetchTime';
        }elseif ($number == 5){
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold  = 'mycat248'; //这是老的数据库;
            $oldtable = 'mws_list_order';
            $newtable = 'mws_list_order';
            $column = 'FetchTime';
        }elseif ($number == 6){
            $mysqlmiged = 'mysql';  //这是转移后的新的数据库
            $mysqlold  = 'mycat248'; //这是老的数据库;
            $oldtable = 'mws_list_order_latest';
            $newtable = 'mws_list_order_latest';
            $column = 'FetchTime';
        }else{
            die("传入数值出错");
        }
        $this->dichotomy2($mysqlmiged,$mysqlold,$oldtable,$newtable,$column);
    }

    //二分法查找数据库转移途中丢失的数据
    public function dichotomy2($mysqlmiged,$mysqlold,$oldtable,$newtable,$column){
        $result = \DB::connection("mysql3310")->table("checktable")
            ->where("oldsql",$mysqlold)->where("newsql",$mysqlmiged)
            ->where("oldtable",$oldtable)->where("newtable",$newtable)
            ->where("column",$column)->where("magnitude",100000)
            ->orderByDesc("end")->first();

        if($result){
            $arr = (array)$result;
            unset($arr['id']);
            $downtime = $result->end;
            $uptiem = $result->endend;
            //倒叙取得第一条（也就是最后一条）
            $result = \DB::connection($mysqlmiged)->table($newtable)->orderByDesc($column)->first();
            if($result){
                $uptiem = $result->$column;
                $arr["endend"] = $uptiem;
            }
            $arr = $this->polling($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, 100000,$column,$arr,'all');
            print_r($arr);
        }else{
            $arr = array();
            $arr["oldsql"] = $mysqlold;
            $arr["newsql"] = $mysqlmiged;
            $arr["oldtable"] = $oldtable;
            $arr["newtable"] = $newtable;
            $arr["column"] = $column;
            echo "start dichotomy" . PHP_EOL;
            //倒叙取得第一条（也就是最后一条）
            $result = \DB::connection($mysqlmiged)->table($newtable)->orderByDesc($column)->first();
            //顺序取得第一条
            $result2 = \DB::connection($mysqlmiged)->table($newtable)->orderBy($column)->first();
            // 判断数据是否存在
            if ($result && $result2) {
                //取出上限时间和下限时间，
                $uptiem = $result->$column;
                $downtime = $result2->$column;
                $arr["endstart"] = $downtime;
                $arr["endend"] = $uptiem;
                echo "first start Upper limit time an The lower limit time is :" . $uptiem . " and " . $downtime . PHP_EOL;
                $numbermy = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>=', $downtime)->where($column, '<=', $uptiem)->count('id');
                echo "first start new mysql number is :" . $numbermy . PHP_EOL;
                echo "start get oldmysql" . PHP_EOL;
                $number248 = \DB::connection($mysqlold)->table($oldtable)->where($column, '>=', $downtime)->where($column, '<=', $uptiem)->count('id');

                //获取整体之间的差值
                echo "first start old mysql number is :" . $number248 . PHP_EOL;
                $numbercha = $number248 - $numbermy;

                $arr["oldnumber"] = $number248;
                $arr["newnumber"] = $numbermy;


                if ($numbercha != 0) {
                    $arr["start"] = $downtime;
                    $arr["end"] = $uptiem;
                    $arr["status"] = 0;
                    $arr["status2"] = 1;
                    $arr["FetchTime"] = time();
                    $arr["poor"] = $numbercha;
                    $arr["magnitude"] = "all";
                    $arr["Onmagnitude"] = '';
                    \DB::connection("mysql3310")->table("checktable")->insert($arr);
                    echo "the poor < 0,so There is an error,please check,the poor is " . $numbercha . PHP_EOL;
                    $arr = $this->polling($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, 100000, $column, $arr,"all");
                    print_r($arr);
                    die("please check");
                } else {
                    $arr["start"] = $downtime;
                    $arr["end"] = $uptiem;
                    $arr["status"] = 1;
                    $arr["status2"] = 1;
                    $arr["FetchTime"] = time();
                    $arr["poor"] = $numbercha;
                    $arr["magnitude"] = "all";
                    $arr["Onmagnitude"] = '';
                    \DB::connection("mysql3310")->table("checktable")->insert($arr);
                }
                //二分法在数据库的具体操作中对数据库的压力太大，先放弃，代码保留
//            if($numbermy == $number248){
//                echo " data is the something, number is".$numbermy.PHP_EOL;
//            }else{
//                echo "The data size is different,poor is".$numbercha.PHP_EOL;
//                $this->checkVariance($mysqlmiged,$mysqlold,$oldtable,$newtable,$column,$downtime,$uptiem,$numbercha);
//            }
            }else{
                echo "data  err at my mycat";
            }
        }
    }


    /**
     * 对数据库中差不是0的数据分片
     */
    public function shard($newsql,$newtable){
        $result = \DB::connection("mysql3310")->table("checktable")
            ->where("poor","!=",0)
            ->where("status2",0)
            ->where("newsql",$newsql);
        if($newtable){
            $result = $result->where("newtable",$newtable);
        }
        $result = $result->get();
        print "please check luoji :".count($result).PHP_EOL;
        if($result->all()){
            foreach ($result as $item){
                $arr = (array)$item;
                unset($arr['id']);

                $length = strlen($item->magnitude) - 2;
                $limit = pow(10,$length);
                if($limit >= 10){
                    $arr = $this->polling($item->newsql, $item->oldsql, $item->oldtable, $item->newtable, $item->start, $item->end, $limit, $item->column, $arr,$item->id);
                    \DB::connection("mysql3310")->table("checktable")->where("id",$item->id)->update(["status2"=>1]);
                }else{
                    echo "小于10，不进行深度对比".PHP_EOL;
                }


            }
        }
    }

    /**
     * 修正数据
     */
    public function revises(){
        $result = \DB::connection("mysql3310")->table("checktable")
            ->where("magnitude",10)->where("status",0)
            ->orderBy("end")->get();
        print "please check luoji :".count($result).PHP_EOL;
        if($result->all()){
            foreach ($result as $item){
                if($item->poor != 0) {
                    echo $item->column . PHP_EOL;
                    $ids = \DB::connection($item->oldsql)->table($item->oldtable)->where($item->column, ">=", $item->start)
                        ->where($item->column, "<=", $item->end)->limit(1000)->get(['id']);
                    $arr = array();
                    foreach ($ids as $id) {
                        $arr[] = $id->id;
                    }
                    print_r(count($arr));
                    echo "***********".PHP_EOL;
                    $ids = \DB::connection($item->newsql)->table($item->newtable)->where($item->column, ">=", $item->start)
                        ->where($item->column, "<=", $item->end)->limit(1000)->get(['id']);
                    $arr2 = array();
                    foreach ($ids as $id) {
                        $arr2[] = $id->id;
                    }
                    print_r(count($arr2));
                    echo "************".PHP_EOL;
                    if ($item->poor < 0) {
                        $arr3 = array_diff($arr2, $arr);
                        if ($arr3) {
                            foreach ($arr3 as $id) {
                                print $id . PHP_EOL;
                                \DB::connection($item->newsql)->table($item->newtable)->where('id', $id)
                                    ->delete();
                            }
                            \DB::connection("mysql3310")->table("checktable")->where("id", $item->id)
                                ->update(["status" => 1]);
                        } else {
                            \DB::connection("mysql3310")->table("checktable")->where("id", $item->id)
                                ->update(["status" => 1]);
                        }
                    }else{
                        $arr3 = array_diff($arr, $arr2);
                        foreach ($arr3 as $id) {
                            print $id . PHP_EOL;
                            $result = \DB::connection($item->oldsql)->table($item->oldtable)->where('id', $id)
                                ->first();
                            $result = get_object_vars($result);
                            if (\DB::connection($item->newsql)->table($item->newtable)->insert($result)) {
                                echo "this asin insert success" . PHP_EOL;
//                                $insertArray = array();
                            }else{
                                echo "insert fail, please check".PHP_EOL;
                            }

                        }
                        \DB::connection("mysql3310")->table("checktable")->where("id", $item->id)
                            ->update(["status" => 1]);
                    }
                }
            }
        }
    }




    /**
     * 出现转移的库比原来的库大的时候，调用改函数进行每段对比
     */
    public function polling($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, $limit,$column,$arr,$id='')
    {
        $downlingshi = $downtime;
        $uplingshi = $uptiem;
        $count = 0;
        while(1){
            echo "count is".$count.PHP_EOL;
            echo "over is".$uptiem.PHP_EOL;
            echo "limit is:",$limit.PHP_EOL;
            $uplingshiresult = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>', $downlingshi)->orderBy($column)->offset($limit)->first();
            print_r($uplingshiresult);
            if ($uplingshiresult) {
                $uplingshi = $uplingshiresult->$column;
                if($uplingshi > $uptiem){
                    $uplingshi = $uptiem;
                }
            }else{
                $uplingshi = $uptiem;
            }
            echo "uplishi and down lishi is :".$uplingshi." and ".$downlingshi.PHP_EOL;
            $numbermy = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            echo "my over".PHP_EOL;
            $number248 = \DB::connection($mysqlold)->table($oldtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            $cha = $number248-$numbermy;
            echo "poor is :".$cha.PHP_EOL;
            $arr["start"] = $downlingshi;
            $arr["end"] = $uplingshi;
            $arr["poor"] = $cha;
            $arr["oldnumber"] = $number248;
            $arr["newnumber"] = $numbermy;
            $arr["magnitude"] = $limit;
            $arr["Onmagnitude"] = $id;
            if($cha == 0){
                $arr["status"] = 1;
                $arr["status2"] = 1;
            }else{
                if($limit <= 10){
                    $arr["status"] = 0;
                    $arr["status2"] = 1;
                }else{
                    $arr["status"] = 0;
                    $arr["status2"] = 0;
                }
            }
            $arr["FetchTime"] = time();
            \DB::connection("mysql3310")->table("checktable")->insert($arr);

//            if($cha != 0){
//                if($limit > 10){
//                    $arrchild =  array($downlingshi,$uplingshi,$number248,$numbermy,0);
//                    $arr[]=$arrchild;
//                    $length = strlen($limit) - 2;
//                    $limit2 = pow(10,$length);
//                    echo "join next polling, limit is ".$limit.PHP_EOL;
//                    $arr[] = $this->polling($mysqlmiged, $mysqlold, $oldtable, $newtable, $downlingshi, $uplingshi, $limit2,$column);
//                    echo "from polling get out".PHP_EOL;
//                    print_r($arr);
//                }else{
//                    $arrchild =  array($downlingshi,$uplingshi,$number248,$numbermy,1);
//                    $arr[]=$arrchild;
//                }
//            }
            if($uplingshi >= $uptiem){
                echo "uplingshi >= uptiem".PHP_EOL;
                break;
            }
//            if($uplingshi == $downlingshi){
//                $uplingshi = $uplingshi+1;
//            }
            $downlingshi = $uplingshi;
            $count = $count+1;
        }
        return $arr;
    }


    /**
     * 数据出现差距，且原始数据大于现在数据，开始调用此函数
     * @param $mysqlmiged
     * @param $mysqlold
     * @param $oldtable
     * @param $newtable
     * @param $column
     * @param $downtime
     * @param $uptiem
     * @param $numbercha
     */
    public function checkVariance($mysqlmiged,$mysqlold,$oldtable,$newtable,$column,$downtime,$uptiem,$numbercha){
        //上限和下限的临时值
        if($uptiem == $downtime){
            die("up and down is something,err time is ".$uptiem.PHP_EOL);
        }
        $uplingshi = 0;
        $downlingshi = 0;
        $status=1;
        //中间的数据点
        $zhongtime=1;
        while (1)
        {
            echo "start while";
            if($status == 1){
                $zhongtime = (int)(($uptiem - $downtime) / 2) + $downtime;  //获取第一次的中间数据点
                echo "middle time is:".$zhongtime.PHP_EOL;
                //使用下限和中间组成临时的上下数据点
                $downlingshi = $downtime;
                $uplingshi = $zhongtime;
            }
            echo "now AD hoc Upper limit time and AD hoc The lower limit time is:".$uplingshi." and ".$downlingshi.PHP_EOL;
            //获取数据量
            $numbermy = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            $number248 = \DB::connection($mysqlold)->table($oldtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            //临时数据点获取的差为
            $numbercha2 = $number248 - $numbermy;
            echo "The data size is different,poor is :".$numbercha2.PHP_EOL;
            if($numbercha2 < 0){
                echo "the poor < 0,so There is an error,please check,the poor is ".$numbercha2.PHP_EOL;
                die("please check");
            }
            if($downlingshi == $uplingshi){
                $arr = $this->slipt($mysqlmiged, $mysqlold, $oldtable, $newtable, $downlingshi, $uptiem,$column);
                die("jie shu beacuse up and down is some");
            }
            if ($numbercha2 == $numbercha) {  //说明差距全在这个范围内
                if($status==1){
                    $uptiem = $zhongtime;
                }else{
                    $uptiem = $uplingshi;
                    $downtime = $downlingshi;
                    $status=1;
                }
                echo "Data size error First half at range (".$downtime.",".$uptiem.")".PHP_EOL;

            } else {  //否则
                if ($numbercha2 == 0) {  //为0代表全部不在这个范围内
                    if($status == 1){
                        if($downtime == $zhongtime){  //防止出现在一个数上循环的情况
                            $downtime = $zhongtime+1;
                        }else{
                            $downtime = $zhongtime;
                        }
                    }else{
                        $uptiem = $uplingshi;
                        $downtime = $downlingshi;
                        if($uptiem-$downtime ==1){
                            $arr = $this->slipt($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem,$column);
                            print_r($arr);
                            die("game over");
                        }
                        $status=1;
                    }
                     echo "Data size error at The second half at range (".$downtime.",".$uptiem.")".PHP_EOL;
                }else {  //否则两边都有
                    echo " the size err at two half ,the times is:".$status.PHP_EOL;
                    if($status > 10){
                        echo "start digui".PHP_EOL;
                        $arr = $this->slipt($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem,$column);
                        print_r($arr);
                        die("game over");
                    }
                    if($numbercha2 >= $numbercha/2){  //如果差值的比例大于总差的1/2，代表此处占多数，移动另一边
                        $uplingshi = $uptiem-(int)(($uptiem - $downtime) / (2*$status));
                        $downlingshi = $downtime;
                        $status=$status+1;
                    }else{
                        $downlingshi = (int)(($uptiem - $downtime) / (2*$status)) + $downtime;
                        $uplingshi = $uptiem;
                        $status=$status+1;
                    }

                }
            }
        }
    }



    /**
     * 数据级别间比对
     */
    public function slipt($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, $column){
        $arr = array();
        $numberliang = \DB::connection($mysqlmiged)->table($newtable)->where('FetchTime','>=',$downtime)->where($column,'<=',$uptiem)->count('id');
        echo "all number count is :".$numberliang.PHP_EOL;
        $length = strlen($numberliang) - 2;
        $limit = pow(10,$length);
        if($limit < 1){
            $limit = 1;
        }
        echo "up and down is".$uptiem.'==>'.$downtime;
        $arr = $this->recursive($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, $limit,$column);

        return $arr;
    }

    /**
     * 递归调用
     */
    public function recursive($mysqlmiged, $mysqlold, $oldtable, $newtable, $downtime, $uptiem, $limit,$column){
        echo "up and down is".$uptiem.'==>'.$downtime.PHP_EOL;
        if($limit > 300000){
            $limit = 300000;
        }
        if($limit < 1000){
            $limit = 1000;
        }
        $arr = array();
        $downlingshi = $downtime;
        while (1){
            echo "start while at recursive".PHP_EOL;
            echo "limit is ".$limit.PHP_EOL;

            echo "downlishi is :".$downlingshi.PHP_EOL;
            echo $count =  \DB::connection($mysqlmiged)->table($newtable)->where($column,'>=',$downlingshi)->where($column,'<=',$uptiem)->count();
            if($count < $limit){
                $uplingshi = $uptiem;
            }else{
                $uplingshiresult = \DB::connection($mysqlmiged)->table($newtable)->where($column,'>=',$downlingshi)->orderBy($column)->offset($limit)->first();
                print_r($uplingshiresult);
                if($uplingshiresult){
                    $uplingshi =$uplingshiresult->FetchTime;
                    if($uplingshi == $downlingshi){
                        $limit=$limit*10;
                        continue;
                    }
                    if($uplingshi > $uptiem){
                        $uplingshi = $uptiem;
                    }
                }else{
                    die("select err ");
                }
                echo "uplishi is".$uplingshi.PHP_EOL;
            }
            $numbermy = \DB::connection($mysqlmiged)->table($newtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            $number248 = \DB::connection($mysqlold)->table($oldtable)->where($column, '>=', $downlingshi)->where($column, '<=', $uplingshi)->count('id');
            $cha = $number248-$numbermy;
            if($cha < 0){
                echo "chu xian xiao yu 0,qingkuang  shujuku zhuanyi cuowu".PHP_EOL;
                die("please check");
            }
            echo "numbermy and number248 is：".$numbermy."==>".$number248.PHP_EOL;
            if($numbermy != $number248){
                $arrchild =  array($downlingshi,$uplingshi,$number248,$numbermy);
                $arr[] = $arrchild;
                if($limit > 1000){
                    $limit2 = $limit/10;
                    echo "start next recursive".PHP_EOL;
                    $arr[] = $this->recursive($mysqlmiged, $mysqlold, $oldtable, $newtable, $downlingshi, $uplingshi, $limit2,$column);
                }else{
                    $this->sqlCompletion($mysqlold,$mysqlmiged,$oldtable,$newtable,$column,$downlingshi,$uplingshi);
                }
            }
            $downlingshi = $uplingshi;
            echo "uplingshi is:".$uplingshi." uptime is :".$uptiem.PHP_EOL;
            if($uplingshi >= $uptiem){
                echo "uplingshi >= uptiem".PHP_EOL;
                break;
            }
        }
        echo "kai shi return ".PHP_EOL;
        return $arr;
    }

    /**
     * 转移某个范围内丢失的数据
     * @return bool
     */
    public function Item248Loss2($oldmysql,$newmysql,$oldtable,$newtable,$column,$down,$up,$limit=5000){
//        $id = 8564194;
        while (1) {
            echo "start id is :".$down.PHP_EOL;
            $results = \DB::connection($oldmysql)->table($oldtable)->where($column, '>', $down)->orderBy($column)->offset(0)->limit($limit)->get();
            print_r(count($results));
            if (count($results)) {
                echo "get 10000 data" . PHP_EOL;
                if ($down > $up) {
                    echo "转移完毕";
                    return false;
                }
            } else {
                echo "转移完毕";
                return false;
            }
            foreach ($results as $result) {
                echo "start foreach".PHP_EOL;
                $result = get_object_vars($result);
//                print_r($result);
                $id = $result['id'];
                print $id.PHP_EOL;
//                print_r($result);
                $number = \DB::connection($newmysql)->table($newtable)->where($column, $id)->count();
                print $number.PHP_EOL;
                if ($number >= 1) {
                    echo "this is being :" . $id . PHP_EOL;
                } else {
                    if (\DB::connection($newmysql)->table($newtable)->insert($result)) {
                        echo "insert success" . PHP_EOL;
                    } else {
                        die("insert err");
                    }
                }
            }
        }
    }


    /**
     * 丢失的数据库补全结构
     * @param $oldmysql  从哪个数据库转移
     * @param $newmysql  转移到哪个数据库
     * @param $oldtable  从哪个表转移
     * @param $newtable  转移到那张表
     * @param $column  判断的字段  FetchTime
     * @param $down  字段下限
     * @param $up   字段上限
     * @param int $limit  转移的跨度
     */
    public function sqlCompletion($oldmysql,$newmysql,$oldtable,$newtable,$column,$down,$up,$limit=1000){
        echo "**********************************************************************".PHP_EOL;
        echo $down."==>".$up.PHP_EOL;
        $results = \DB::connection($oldmysql)->table($oldtable)->where($column,'>=',$down)->where($column,'<=',$up)->orderBy('id')->offset(0)->limit($limit)->get();
        echo count($results);
        if(count($results)){
            foreach ($results as $result){
                $result = get_object_vars($result);
//                print_r($result);
                $id = $result['id'];
                print $id.PHP_EOL;
//                print_r($result);
                $number = \DB::connection($newmysql)->table($newtable)->where('id', $id)->count();
                print $number.PHP_EOL;
                if ($number >= 1) {
                    echo "this is being :" . $id . PHP_EOL;
                } else {
                    echo "###################################################################".$id.PHP_EOL;
                    if (\DB::connection($newmysql)->table($newtable)->insert($result)) {
                        echo "insert success" . PHP_EOL;
                    } else {
                        die("insert err");
                    }
                }
            }
        }
    }


}
